﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class MyHtmlHelper
    {
        /// <summary>
        /// 分页控件
        /// </summary>
        /// <param name="url">请求路径</param>
        /// <param name="currentPage">当前页数</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="searchUrl">其他查询条件</param>
        /// <returns></returns>
        public static string ShowPageNavigate(string url,int currentPage, int pageSize, long totalCount,string searchUrl)
        {
            string redirectTo = url.Split('?')[0];
            pageSize = pageSize == 0 ? 3 : pageSize;
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
            var output = new StringBuilder();
            output.Append("<div class='pagination'> <ul> ");
            if (totalPages > 1)
            {
                if(currentPage>1)
                {
                    output.AppendFormat("<li><a href='{0}?page=1{1}'>首页</a></li>", redirectTo, searchUrl);
                    output.AppendFormat("<li><a  href='{0}?page={1}{2}'>上一页</a> </li>", redirectTo, currentPage - 1, searchUrl);
                }
                else
                {
                    currentPage = 1;
                }

                output.Append(" ");
                int currint = 5;
                for (int i = 0; i <= 10; i++)
                {//一共最多显示10个页码，前面5个，后面5个
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                    {
                        if (currint == i)
                        {//当前页处理                           
                            output.AppendFormat("<li class='active'><a href='#'>{0}</a></li> ",currentPage);
                        }
                        else
                        {//一般页处理
                            output.AppendFormat("<li><a href='{0}?page={1}{2}'>{1}</a></li> ", redirectTo, currentPage + i - currint, searchUrl);
                        }
                    }
                    output.Append(" ");
                }
                if (currentPage < totalPages)
                {//处理下一页的链接
                    output.AppendFormat("<li><a href='{0}?page={1}{2}'>下一页</a></li> ", redirectTo, currentPage + 1, searchUrl);
                }

                output.Append(" ");
                if (currentPage != totalPages)
                {
                    output.AppendFormat("<li><a href='{0}?page={1}{2}'>末页</a></li> ", redirectTo, totalPages, searchUrl);
                }
                output.Append(" ");
            }
            output.Append("</ul></div> ");

            return output.ToString();
        }

    }
}
